package com.eduhzy.ttw.teacher.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.ResourceUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.DiscoverData;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.eduhzy.ttw.commonsdk.imgaEngine.GlideImageLoader;
import com.eduhzy.ttw.teacher.R;
import com.eduhzy.ttw.teacher.di.component.DaggerDiscoverComponent;
import com.eduhzy.ttw.teacher.mvp.contract.DiscoverContract;
import com.eduhzy.ttw.teacher.mvp.presenter.DiscoverPresenter;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.youth.banner.Banner;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

;

/**
 * @author xubo
 * @date 2018-7-20 15:33:28
 */
@Route(path = RouterHub.APP_DISCOVERFRAGMENT)
public class DiscoverFragment extends BaseFragment<DiscoverPresenter> implements DiscoverContract.View {
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout mSwipeLayout;
    @Inject
    RecyclerView.LayoutManager layoutManager;
    @Inject
    BaseQuickAdapter<DiscoverData, AutoBaseViewHolder> mAdapter;
    private View header;
    private Banner banner;

    public static DiscoverFragment newInstance() {
        DiscoverFragment fragment = new DiscoverFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerDiscoverComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_discover, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        rvList.setLayoutManager(layoutManager);
        rvList.setAdapter(mAdapter);
        addOrRefreshHeaderView();
        getData(true);
        mAdapter.setOnLoadMoreListener(() -> getData(false), rvList);
        mSwipeLayout.setOnRefreshListener(() -> {
            getData(true);
        });
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            ARouter.getInstance().build(RouterHub.APP_WEBVIEWACTIVITY)
                    .withString(Intent.EXTRA_TITLE, "资讯详情")
                    .withString(Constants.ROUTER_STRING, mAdapter.getData().get(position).getUrl())
                    .navigation();
        });
    }

    private void getData(boolean pullToRefresh) {
        mPresenter.getContentList(pullToRefresh);
    }

    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void showLoading() {
        if (ObjectUtils.isNotEmpty(mSwipeLayout)) {
            mSwipeLayout.setRefreshing(true);
        }
    }

    @Override
    public void hideLoading() {
        if (ObjectUtils.isNotEmpty(mSwipeLayout)) {
            mSwipeLayout.setRefreshing(false);
        }
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    private void addOrRefreshHeaderView() {
        mAdapter.setHeaderAndEmpty(true);
        List<String> list = new ArrayList<>();
        File img = new File(getActivity().getExternalCacheDir(), "img_discovery.png");
        ResourceUtils.copyFileFromAssets("img_discovery.png", img.getPath());
        list.add(img.getPath());
        if (header == null) {
            header = getLayoutInflater().inflate(R.layout.discover_banner_header, null);
            this.banner = header.findViewById(R.id.banner);
            mAdapter.addHeaderView(header);
        }

        this.banner.setImages(list)
                .setImageLoader(new GlideImageLoader())
                .setOnBannerListener(position -> {
                        }
                )
                .start();

    }

    @Override
    public void killMyself() {

    }
}
